package com.team7.happycommunity.parkservice.service.impl;

import com.team7.happycommunity.parkservice.dao.ParkingRentRecordsMapper;
import com.team7.happycommunity.parkservice.dao.PersonUserMapper;
import com.team7.happycommunity.parkservice.dto.MyParkingRecordsDTO;
import com.team7.happycommunity.parkservice.dto.ParkingRentDTO;
import com.team7.happycommunity.parkservice.dto.ParkingRentDateDTO;
import com.team7.happycommunity.parkservice.pojo.ParkingRentRecords;
import com.team7.happycommunity.parkservice.pojo.PersonUser;
import com.team7.happycommunity.parkservice.service.ParkingRentRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ParkingRentRecordsServiceImpl implements ParkingRentRecordsService {

    @Autowired
    ParkingRentRecordsMapper parkingRentRecordsMapper;

    @Autowired
    PersonUserMapper personUserMapper;

    @Override
    public List<ParkingRentDateDTO> getRecords(Integer id) {
        List<ParkingRentRecords> list = parkingRentRecordsMapper.selectByParkingInfoId(id);
        List<ParkingRentDateDTO> list_re = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i = 0; i < list.size() ; i ++){
            ParkingRentDateDTO parkingRentDateDTO = new ParkingRentDateDTO();
            parkingRentDateDTO.setBeginT(simpleDateFormat.format(list.get(i).getBeginTime()));
            parkingRentDateDTO.setEndT(simpleDateFormat.format(list.get(i).getEndTime()));
            list_re.add(parkingRentDateDTO);
        }
        return list_re;
    }

    @Override
    public int add(ParkingRentDTO parkingRentDTO) {
        PersonUser personUser = personUserMapper.selectByUsername(parkingRentDTO.getUserName());

        ParkingRentRecords parkingRentRecords = new ParkingRentRecords();
        parkingRentRecords.setBeginTime(parkingRentDTO.getBeginTime());
        parkingRentRecords.setEndTime(parkingRentDTO.getEndTime());
        parkingRentRecords.setCarid(parkingRentDTO.getCarId());
        parkingRentRecords.setStatus(0);
        parkingRentRecords.setParkingInfoId(parkingRentDTO.getParkingInfoId());
        parkingRentRecords.setUserId(personUser.getId());
        return parkingRentRecordsMapper.insertSelective(parkingRentRecords);

//        return 0;
    }

    @Override
    public List<MyParkingRecordsDTO> getMyRecords(String username) {
        PersonUser personUser = personUserMapper.selectByUsername(username);
        List<MyParkingRecordsDTO>  list = parkingRentRecordsMapper.selectByUserId(personUser.getId());
        return list;
    }
}
